package Chuong04;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.jws.soap.SOAPBinding.Use;
import javax.swing.*;

public class Bai03 extends JFrame implements ActionListener{
	
	String[][] User;
	JButton btnSub = new JButton("Đăng nhập");
	JLabel lblTitble = new JLabel("ĐĂNG NHẬP");
	JLabel lblDN = new JLabel("Tên đăng nhập:");
	JLabel lblMK = new JLabel("Mật khẩu:");
	JTextField txtTDN = new JTextField();
	JPasswordField txtMK = new JPasswordField();
	
	public Bai03(){
		setSize(300, 180);
	    setTitle("Bài 03 - Chương 04");
	    btnSub.addActionListener(this);
	    Container container = getContentPane();
	    
		User = new String[5][2];
		User[0][0] = "vvantay";
		User[0][1] = "vantay";
		
		User[1][0] = "vvtay";
		User[1][1] = "vantay";
		
		User[2][0] = "tmatai";
		User[2][1] = "anhtaitruong";
		
		User[3][0] = "dvtra";
		User[3][1] = "vantra";
		
		User[4][0] = "admin";
		User[4][1] = "admin";
		
		JPanel p1 = new JPanel();
		lblTitble.setFont(new Font(getName(), Font.BOLD, 20));
		p1.add(lblTitble, BorderLayout.CENTER);
		
		JPanel p2 = new JPanel();
		p2.setLayout(new GridLayout(2, 1));
		
		JPanel p3 = new JPanel();
		p3.setLayout(new GridLayout(2, 2));
		p3.add(lblDN); p3.add(txtTDN);
		p3.add(lblMK); p3.add(txtMK);
		
		JPanel p4 = new JPanel();
		p4.add(btnSub);
		
		p2.add(p3);
		p2.add(p4);
		
		container.add(p1, BorderLayout.NORTH);
        container.add(p2, BorderLayout.CENTER);
	}
	
	public static void main(String[] args) {
		Bai03 frm = new Bai03();
		frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frm.setVisible(true);

	}
	
	public boolean isUser(String tdn, String mk){
		boolean resuft = false;
		String t =  new String();
		for(int i = 0; i < 5; i++){
			if(tdn.compareTo(User[i][0]) == 0 && mk.compareTo(User[i][1]) == 0)
				resuft = true;
			t += User[i][0] +"-" + User[i][1] +"\n";
		}
		//JOptionPane.showMessageDialog(this,t, "Thông báo",JOptionPane.INFORMATION_MESSAGE);
		return resuft;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnSub){
			String tdn = txtTDN.getText();
			String mk = txtMK.getText();
			if(isUser(tdn, mk)){
				JOptionPane.showMessageDialog(this,"Đăng nhập thành công!", "Thông báo",JOptionPane.INFORMATION_MESSAGE);
					
			}else{
				JOptionPane.showMessageDialog(this,"Tên đăng nhập hoặc mật khẩu không đúng!", "Lỗi",JOptionPane.ERROR_MESSAGE);
			}
		}		
	}

}
